package com.springboot.learn_zookeeper.demo2;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * 判断节点是否存在
 */
public class ZKExists {
    private static ZooKeeper zk;
    private static ZooKeeperConnection connection;

    public static Stat znode_exists(String path) throws KeeperException, InterruptedException {
        return zk.exists(path,true);
    }

    public static void main(String[] args){
        String path="/MyFirstZnode";
        try {
            connection=new ZooKeeperConnection();
            zk=connection.connect("localhost");
            Stat stat=znode_exists(path);
            if (stat!=null){
                System.out.println("Node exists and the node version is "+stat.getVersion());
            }else {
                System.out.println("Node does not exists");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
